package com.heima.controller;

import com.heima.annotation.GlobalInterceptor;
import com.heima.entity.dto.SessionWebUserDto;
import com.heima.entity.enums.FileDelFlagEnums;
import com.heima.entity.po.FileInfo;
import com.heima.entity.query.FileInfoQuery;
import com.heima.entity.vo.FileInfoVO;
import com.heima.entity.vo.PaginationResultVO;
import com.heima.entity.vo.ResponseVO;
import com.heima.service.FileInfoService;
import com.heima.service.RecycleService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/recycle")
public class RecycleController extends ABaseController{
    @Resource
    private RecycleService recycleService;

    @Resource
    private FileInfoService fileInfoService;
    @RequestMapping("/loadRecycleList")
    @GlobalInterceptor
    public ResponseVO loadRecycleList(HttpSession session,
                                      Integer pageNo,
                                      Integer pageSize){
        SessionWebUserDto webUserDto = getUserInfoFromSession(session);

        FileInfoQuery fileInfoQuery = new FileInfoQuery();
        fileInfoQuery.setPageNo(pageNo);
        fileInfoQuery.setPageSize(pageSize);
        fileInfoQuery.setDelFlag(FileDelFlagEnums.RECYCLE.getFlag());
        fileInfoQuery.setUserId(webUserDto.getUserId());
        fileInfoQuery.setOrderBy("folder_type desc, last_update_time desc");
        PaginationResultVO<FileInfo> listByPage = fileInfoService.findListByPage(fileInfoQuery);
        return getSuccessResponseVO(convert2PaginationVO(listByPage, FileInfoVO.class));
    }

    @RequestMapping("/recoverFile")
    @GlobalInterceptor
    public ResponseVO recoverFile(HttpSession session,String fileIds){
        SessionWebUserDto webUserDto = getUserInfoFromSession(session);
        recycleService.recoverFile(webUserDto.getUserId(),fileIds);
        return getSuccessResponseVO(null);
    }
    @RequestMapping("/delFile")
    @GlobalInterceptor
    public ResponseVO delFile(HttpSession session,String fileIds){
        SessionWebUserDto webUserDto = getUserInfoFromSession(session);
        recycleService.delFile4ever(webUserDto.getUserId(),fileIds);
        return getSuccessResponseVO(null);
    }
}
